<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;
/**
 * 后台用户控制器
 * @author jry <598821125@qq.com>
 */
class MemberController extends AdminController{
    /**
     * 用户列表
     * @author jry <598821125@qq.com>
     */
    public function index(){
        //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        $map['name|email|tel'] = array($condition, $condition, $condition,'_multi'=>true);

        
        $data_list = D('Member')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('Member')->where($map)->count(), C('ADMIN_PAGE_ROWS'));

        //使用Builder快速建立列表页面。
        $builder = new \Common\Builder\ListBuilder();
        $builder->setMetaTitle('會員列表') //设置页面标题
                ->addTopButton('addnew')  //添加新增按钮
                ->addTopButton('delete')  //添加删除按钮
                ->setSearch('請輸入ID/用戶名/郵箱/手機號', U('index'))
                ->addTableColumn('name', '用戶名')
                ->addTableColumn('address', '聯繫地址')
                ->addTableColumn('email', '郵箱')
                ->addTableColumn('tel', '手機號')
                ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) //数据列表
                ->setTableDataPage($page->show()) //数据列表分页
                ->addRightButton('edit')   //添加编辑按钮
                ->addRightButton('forbid') //添加禁用/启用按钮
                ->addRightButton('delete') //添加删除按钮
                ->display();
    }

    /**
     * 新增用户
     * @author jry <598821125@qq.com>
     */
    public function add(){
        if(IS_POST){
            $user_object = D('Member');
            $data = $user_object->create();
            if($data){
                $id = $user_object->add();
                if($id){
                    $this->success('新增成功', U('index'));
                }else{
                    $this->error('新增失敗');
                }
            }else{
                $this->error($user_object->getError());
            }
        }else{
            $user_object = D('Member');

            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('新增用戶') //设置页面标题
                    ->setPostUrl(U('add')) //设置表单提交地址
                    ->addFormItem('name', 'text', '用戶名', '用戶名')
                    ->addFormItem('pwd', 'password', '密碼', '密碼')
                    ->addFormItem('repwd', 'password', '確認密碼', '確認密碼')
                    ->addFormItem('email', 'text', '聯繫地址', '聯繫地址')
                    ->addFormItem('address', 'text', '郵箱', '郵箱')
                    ->addFormItem('tel', 'text', '手機號碼', '手機號碼')
                    ->display();
        }
    }

    /**
     * 编辑用户
     * @author jry <598821125@qq.com>
     */
    public function edit($id){
        //获取用户信息
        $info = D('Member')->find($id);

        if(IS_POST){
            $user_object = D('Member');
            //不修改密码时销毁变量
            if($_POST['pwd'] == '' || $info['pwd'] == $_POST['pwd']){
                unset($_POST['pwd']);
            }else{
                $_POST['pwd'] = user_md5($_POST['pwd']);
            }
            if($user_object->save($_POST)){
                $this->success('更新成功', U('index'));
            }else{
                $this->error('更新失敗', $user_object->getError());
            }
        }else{
            $user_object = D('Member');
            $info = $user_object->find($id);
            //使用FormBuilder快速建立表单页面。
            $builder = new \Common\Builder\FormBuilder();
            $builder->setMetaTitle('编辑用戶') //设置页面标题
                    ->setPostUrl(U('edit')) //设置表单提交地址
                    ->addFormItem('id', 'hidden', 'ID', 'ID')
                    ->addFormItem('name', 'text', '用戶名', '用戶名')
                    ->addFormItem('pwd', 'password', '密碼', '密碼')
                    ->addFormItem('email', 'text', '郵箱', '郵箱')
                    ->addFormItem('tel', 'text', '手機號碼', '手機號碼')
                    ->addFormItem('address', 'text', '聯繫地址', '聯繫地址')
                    ->setFormData($info)
                    ->display();
        }
    }
}
